<?php

namespace App\Admin\Controllers\Service;

use App\Models\Common\Media;
use App\Models\Open\Platform;
use App\Models\Open\SysAccount;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\Admin\Controllers\BaseAdminController;
use Illuminate\Support\Str;

class SysAccountController extends BaseAdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(SysAccount::with(['platform', 'appType']), function (Grid $grid) {
            $grid->column('platform.name', admin_trans_field('platform_name'));
            $grid->column('appType.name', admin_trans_field('app_type_name'));
            $grid->column('name');
            $grid->column('description');
            $grid->column('logoid')->display(function ($logoid) {
                return $logoid ? Media::where('id', $logoid)->pluck('url')->toArray() : [];
            })->image('', 50);
            $grid->column('qrcode')->display(function ($qrcode) {
                return $qrcode ? Media::where('id', $qrcode)->pluck('url')->toArray() : [];
            })->image('', 50);
            $grid->column('appid');
            $grid->column('appsecret');
            $grid->column('token');
            $grid->column('aes_key');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['platform.name', 'appType.name', 'name', 'description']);

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append('<a href="'.admin_url('sys-account-menu').'?said='.$this->id.'">菜单</a> ');
                $actions->append('<a href="'.admin_url('sys-account-keyword-reply').'">关键字回复</a> ');
                $actions->append('<a href="'.admin_url('sys-account-follow-reply').'">关注回复</a> ');
                $actions->append('<a href="'.admin_url('sys-account-qrcode-reply').'">二维码回复</a> ');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, SysAccount::with(['platform', 'appType']), function (Show $show) {
            $show->field('platform.name', admin_trans_field('platform_name'));
            $show->field('appType.name', admin_trans_field('app_type_name'));
            $show->field('name');
            $show->field('description');
            $show->field('logoid');
            $show->field('appid');
            $show->field('appsecret');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(SysAccount::with(['platform', 'appType']), function (Form $form) {
            $form->disableViewButton();
            $form->select('platform_id', admin_trans_field('platform_name'))
                ->options(function () {
                    return Platform::all()->pluck('name', 'id');
                })->load('app_type_id', 'app-type-search');
            $form->select('app_type_id', admin_trans_field('app_type_name'));
            $form->text('name');
            $form->textarea('description');
            $form->uploadimagepro('logoid');
            $form->uploadimagepro('qrcode');
            $form->text('appid');
            $form->text('appsecret');
        })->saved(function (Form $form) {
            $up = [];
            if ($form->model()->appid != $form->appid
                || $form->model()->appsecret != $form->appsecret) {
                $up['access_token'] = '';
            }
            if (!$form->model()->token) {
                $up['token'] = Str::random(32);
            }
            if (!$form->model()->aes_key) {
                $up['aes_key'] = Str::random(43);
            }
            $form->model()->update($up);
        });
    }
}
